[db7908]: / Ensemble Learning / Boosting / gradient_boosting_predict.m

Download this file

56 lines (53 with data), 1.9 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
% Practicum, Task #3, 'Compositions of algorithms'.
%
% FUNCTION:
% [prediction, err] = gradient_boosting_predict (model, X, y)
%
% DESCRIPTION:
% This function use the composition of algorithms, trained with gradient
% boosting method, for prediction.
%
% INPUT:
% X --- matrix of objects, N x K double matrix, N --- number of objects,
% K --- number of features.
% y --- vector of answers, N x 1 double vector, N --- number of objects. y
% can have only two values --- +1 and -1.
% model --- trained composition.
%
% OUTPUT:
% prediction --- vector of predicted answers, N x 1 double vector.
% error --- the ratio of number of correct answers to number of objects on
% each iteration, num_iterations x 1 vector
%
% AUTHOR:
% Murat Apishev (great-mel@yandex.ru)
%
function [prediction, err] = gradient_boosting_predict (model, X, y)
num_iterations = length(model.weights);
no_objects = length(y);
pred_prediction = zeros([no_objects num_iterations]);
for alg = 1 : num_iterations
value = zeros([no_objects 1]) + model.b_0;
for i = 1 : alg
if strcmp(model.algorithm, 'epsilon_svr')
value = value + svmpredict(y, X, model.models{i}) * model.weights(i);
elseif strcmp(model.algorithm, 'regression_tree')
value = value + predict(model.models{i}, X) * model.weights(i);
end
end
pred_prediction(:,alg) = value;
end
prediction = pred_prediction(:,end);
err = zeros([num_iterations 1]);
if strcmp(model.loss, 'absolute')
temp = (bsxfun(@minus, pred_prediction, y));
err = abs(sum(temp)) / no_objects;
elseif strcmp(model.loss, 'logistic')
prediction = sign(prediction);
temp = (bsxfun(@eq, sign(pred_prediction), y));
err = sum(temp == 0) / no_objects;
end
if size(err, 1) == 1
err = err';
end
end